<?php
require_once ('odal/meta/complexattribute.php');
require_once ('odal/element.php');
require_once ('odal/targetvector.php');


/**
 * @version 1.0
 * @created 27-Dez-2003 21:35:24
 */
class odal_Relation extends odal_Node{

	var $m_sourceElement;
	var $m_targetVector;

	function odal_Relation( &$sourceElement, &$targetElement, $catName, $type, $relId, $key, $order, $path = null, $level = null, $state = 0 ){
		$this->_createRelation( $sourceElement, $targetElement, $catName, $type, $relId, $key, $order, $path, $level, $state );
	}//Relation

	function _createRelation( &$sourceElement, &$targetElement, $catName, $type, $relId, $key, $order, $path, $level, $state ){
//		echo '[relation->_createRelation] path: *' . $path . "*<br>\n";
		$this->m_sourceElement =& $sourceElement;
		$this->m_targetVector = new odal_TargetVector( $sourceElement, $targetElement, $relId, $key, $order, $path, $level, $state );
		$this->m_id =& $catName;
		$this->m_name =& $type;
		if ( $type == "c" ){
			//echo "(relation->_createRelation) sourceElement->getId(): " . $sourceElement->getId() . "<br>";
			//new element can only be composed under existing elements
			if ( $state == 1 ){
				$targetElement->setParentElementId( $sourceElement->getId() );
			}//if;
		}//if
		//echo "(Relation->_createRelation) type: ". $type . " - source: ". $sourceElement->getName() .", target: " . $targetElement->getName().", id: " . $relId . "<br>";
	}//Relation
	
	function _pck_isComposition(){
		$bRet = false;
		if ( $this->getName() == "c"){
			$bRet = true;
		}//if
		return $bRet;
	}//_pck_isComposition

	function _pck_isAssociation(){
		return !( $this->_pck_isComposition() );		
	}//_pck_isAssociation

	function _pck_appendElement2Relation( &$element, $relId, $key, $order, $state = 0 ){
		$this->m_targetVector->_pck_appendTarget( $this->m_sourceElement, $element, $relId, $key, $order, null,null,$state );
		//set parentElementId only if element is new 
		if ( ( $this->_pck_isComposition() ) && ( $element->_pck_getState() == 1 ) ){
			//$element->setParentElementId( $this->m_sourceElement->getId() );
		}//if
	}//_pck_appendElement2Relation

	function &_pck_getTargetByRelationId( $id ){
		$retTarget = null;
		$this->m_targetVector->_pck_reset();
		while ( $target =& $this->m_targetVector->_pck_each() ){
			if ( $target->_pck_getRelationId() == $id ){
				$retTarget =& $target;
				break;
			}//if
		}//while
		return $retTarget;
	}//_pck_getTargetByRelationId

	function &_pck_getTargetElementById( $p_id ){
		$retelm = null;
		$this->m_targetVector->_pck_reset();
		//echo "[relation->_pck_getTargetElementById] p_id: " . $p_id . "<br>";
		while ( $target =& $this->m_targetVector->_pck_each() ){
			$targetElement =& $target->_pck_getElement();
			//echo "[relation->_pck_getTargetElementById] targetElement->getId(): " . $targetElement->getId() . " - targetElement->getName: " . $targetElement->getName() . "<br>";
			if ( $targetElement->getId() == $p_id ){
				$retelm =& $targetElement;
				break;
			}//if
		}//while
		return $retelm;
	}//_pck_getTargetElementById

	function &_pck_getTargetByElement( &$p_oElement ){
		$oRetTarget = null;
		$this->m_targetVector->_pck_reset();
		while ( $target =& $this->m_targetVector->_pck_each() ){
			$targetElement =& $target->_pck_getElement();
//			echo "[relation->_pck_getTargetByElement] targetElement->getId(): " . $targetElement->getId() . " <br>";
//			echo "[relation->_pck_getTargetByElement] p_oElement->getId(): " . $p_oElement->getId() . " <br>";
			if ( $targetElement->getId() == $p_oElement->getId() ){
				$oRetTarget =& $target;
				break;
			}//if
		}//while
		return $oRetTarget;
	}//_pck_getTargetElementById
	
	function _pck_isRelated( &$p_oElement ){
		return ( $this->_pck_getTargetByElement( $p_oElement ) != null );
	}//_pck_isRelated

	function &_pck_getTargets(){
		//echo "[relation->_pck_getTargets] <br>";
		$this->m_targetVector->_pck_reset();
		return $this->m_targetVector;
	}//_pck_getTargets
	
	function _pck_getCatName(){
		return $this->getId();
	}//_pck_getCatName

	function _pck_moveElementBefore( &$element, &$beforeElement ){
		//echo "[relation->_pck_moveElementBefore] beforeElement->getId(): " . $beforeElement->getId() . "<br>";
		//$this->m_targetVector->_dispayTargets();
		$elementTarget =& $this->_pck_getTargetByElement( $element );
		$beforeElementTarget =& $this->_pck_getTargetByElement( $beforeElement );
		$this->m_targetVector->_pck_reset();
		$this->m_targetVector->_pck_setCurrent( $beforeElementTarget );
		//echo "[relation->_pck_moveElementBefore] vor  _pck_getPrevious<br>";
		$oPreviousTarget =& $this->m_targetVector->_pck_getPrevious();
		$newOrder = ( $beforeElementTarget->_pck_getRelationOrder() - 1 );
		//echo "[relation->_pck_moveElementBefore] oPreviousTarget->_pck_getRelationId(): " . $oPreviousTarget->_pck_getRelationId() . " <br>";
		//echo "[relation->_pck_moveElementBefore] beforeElementTarget->_pck_getRelationId(): " . $beforeElementTarget->_pck_getRelationId() . " <br>";
		if ( $oPreviousTarget->_pck_getRelationId() != $beforeElementTarget->_pck_getRelationId() ){
			$newOrder = (  ( $oPreviousTarget->_pck_getRelationOrder() ) + ( $beforeElementTarget->_pck_getRelationOrder() ) ) / 2;
		}//if
		//echo "[relation->_pck_moveElementBefore] newOrder: " . $newOrder . " <br>";
		//echo "[relation->_pck_moveElementBefore] elementTarget->_pck_getRelationId(): " . $elementTarget->_pck_getRelationId() . " <br>";
		$this->m_targetVector->_pck_moveTarget( $elementTarget, $newOrder );
		$elementTarget->setState( 4 );
		//$this->m_targetVector->_dispayTargets();
	}//_pck_moveElementBefore

	function _pck_moveElementAppend( &$element ){
		$elementTarget =& $this->_pck_getTargetByElement( $element );
		$oLastTarget =& $this->m_targetVector->_pck_getLast();
		$newOrder = ( $oLastTarget->_pck_getRelationOrder() + 1 );
		$this->m_targetVector->_pck_moveTarget( $elementTarget, $newOrder );
		$elementTarget->setState( 5 );
	}//_pck_moveElementAppend


	function __TODO__(){}
	function getTargetElementByKey()
	{
	}
	function insertElementBefore()
	{
	}

}//odal_Relation
?>